//
//  DeviceEditView.m
//  Kids
//
//  Created by LiTengFei on 14/11/26.
//  Copyright (c) 2014年 90's Technology Co.Ltd. All rights reserved.
//

#import "DeviceEditView.h"
@interface DeviceEditView()

@property (nonatomic,strong) UIView * backgroundView;

@property (nonatomic,strong) UIImageView * headerImageView;
@property (nonatomic,strong) UIButton * editButton;

@property (nonatomic,strong) UIButton * submitButton;
@property (nonatomic,strong) UIButton * cancelButton;

@end
@implementation DeviceEditView

-(instancetype)init{
    if(self = [super init]){
        [self commentInstace];
    }
    return self;
}

-(void)commentInstace{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
//    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
//    
//    [self addGestureRecognizer:tapGes];
    
    self.backgroundView = [[UIView alloc]init];
    
    self.backgroundView.layer.cornerRadius = 30;
    self.backgroundView.backgroundColor = [UIColor whiteColor];
    
    self.backgroundView.frame = CGRectMake(10, 100, 300, 200);
    
    UIImage * line  = [UIImage imageNamed:@"line1"];
    
    self.headerImageView = ({
        UIImageView * imageView= [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 50, 50)];
        imageView.image = [UIImage imageNamed:@"photo_size"];
        imageView;
    });
    self.editButton = ({
        
        UIButton * button  = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 10)];
        [button setBackgroundImage:[UIImage imageNamed:@"edit_words"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"edit_words_press"] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
        button.center = CGPointMake(45, 80);
        button;
    });
    
    self.nameTextField = ({
        UITextField * textfield =  [[UITextField alloc]initWithFrame:CGRectMake(80, 20, CGRectGetWidth(self.backgroundView.frame) - 80 - 20, 20)];
        textfield.placeholder = @"名称";
        textfield.textAlignment = NSTextAlignmentLeft;
        textfield;
    });
   
    UIImageView * lineView = [[UIImageView alloc]initWithFrame:CGRectMake(70, CGRectGetMaxY(self.nameTextField.frame)+3, CGRectGetWidth(self.nameTextField.frame), 1)];
    lineView.image = line;
    [self.backgroundView addSubview:lineView];
    
    self.phoneTextField = ({
        UITextField * textfield =  [[UITextField alloc]initWithFrame:CGRectMake(80, 60, CGRectGetWidth(self.backgroundView.frame) - 80 - 20, 20)];
        textfield.placeholder = @"号码";
//        textfield.textColor = [UIColor lightGrayColor];
        textfield.textAlignment = NSTextAlignmentLeft;
        textfield;
    });
    
    UIImageView * lineView1 = [[UIImageView alloc]initWithFrame:CGRectMake(70, CGRectGetMaxY(self.phoneTextField.frame)+3, CGRectGetWidth(self.phoneTextField.frame), 1)];
    lineView1.image = line;
    [self.backgroundView addSubview:lineView1];
    
    self.passwordTextField = ({
        UITextField * textfield =  [[UITextField alloc]initWithFrame:CGRectMake(80, 100, CGRectGetWidth(self.backgroundView.frame) - 80 - 20, 20)];
        textfield.placeholder = @"密码";
//        textfield.textColor = [UIColor lightGrayColor];
        textfield.textAlignment = NSTextAlignmentLeft;
        textfield;
    });
    UIImageView * lineView2 = [[UIImageView alloc]initWithFrame:CGRectMake(70, CGRectGetMaxY(self.passwordTextField.frame)+3, CGRectGetWidth(self.passwordTextField.frame), 1)];
    lineView2.image = line;
    [self.backgroundView addSubview:lineView2];
    
    
    self.submitButton =  ({
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(20, 140, 110, 36)];
        [button setBackgroundImage:[UIImage imageNamed:@"submit"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"submit_press"] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    
    self.cancelButton = ({
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(170, 140, 110, 36)];
        [button setBackgroundImage:[UIImage imageNamed:@"cancel_button_small"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"cancel_button_small_press"] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    
    
    [self.backgroundView addSubview:self.headerImageView];
    [self.backgroundView addSubview:self.editButton];
    
    [self.backgroundView addSubview:self.nameTextField];
    [self.backgroundView addSubview:self.phoneTextField];
    [self.backgroundView addSubview:self.passwordTextField];
    
    [self.backgroundView addSubview:self.submitButton];
    [self.backgroundView addSubview:self.cancelButton];

    [self addSubview:self.backgroundView];
    
    [self addNotification];
}
-(void)setEditer:(BOOL)editer{
    _editer = editer;
    if(editer){
        self.phoneTextField.enabled = NO;
        self.passwordTextField.enabled = NO;
    }else{
        self.phoneTextField.enabled = YES;
        self.passwordTextField.enabled = YES;
    }
}
-(void)setHeaderImage:(UIImage *)headerImage{
    _headerImage = headerImage;
    self.headerImageView.image = headerImage;
}
//-(void)tap{
//    [self dismiss];
//}
-(void)editClick:(UIButton *)button{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(didSelectedEditButtonInDeviceEditView:)]){
        [self.delegate didSelectedEditButtonInDeviceEditView:self];
    }
//    if(self.delegate && [self.delegate respondsToSelector:@selector(editHeaderImage)]){
//        [self.delegate editHeaderImage];
//    }
}
-(void)dealloc{
    [self removeNotification];
}
-(void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)removeNotification{
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification];
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification];
}
-(void)keyboardShow:(NSNotification *)notification{
    
    NSDictionary* info = [notification userInfo];
    CGSize keyBoardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.backgroundView.frame = CGRectMake(CGRectGetWidth(frame)/2-150, (CGRectGetHeight(frame)-200 - keyBoardSize.height)/2, 300, 200);
    
}
-(void)keyboardHide:(NSNotification *)notification{
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.backgroundView.frame = CGRectMake(CGRectGetWidth(frame)/2-150, (CGRectGetHeight(frame)-200)/2, 300, 200);
}
-(void)setModel:(WatchModel *)model{
    _model = model;
    if(model.wname){
        self.nameTextField.text = model.wname;
    }
    if(model.wphone){
        self.phoneTextField.text = model.wphone;
    }
    if(model.wpwd){
        self.passwordTextField.text = model.wpwd;
    }
}

-(void)show{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.frame = frame;
    self.backgroundView.frame = CGRectMake(CGRectGetWidth(frame)/2-150, (CGRectGetHeight(frame)-200)/2, 300, 200);
    [window addSubview:self];
}
-(void)showInView:(UIViewController *)view{
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.frame = frame;
    self.backgroundView.frame = CGRectMake(CGRectGetWidth(frame)/2-150, (CGRectGetHeight(frame)-200)/2, 300, 200);
    [view.view addSubview:self];
}

-(void)dismiss{
    if([self.nameTextField isFirstResponder]){
        [self.nameTextField resignFirstResponder];
    }
    if([self.phoneTextField isFirstResponder]){
        [self.phoneTextField resignFirstResponder];
    }
    if([self.passwordTextField isFirstResponder]){
        [self.passwordTextField resignFirstResponder];
    }
    [self removeFromSuperview];
    
}
-(void)submitClick:(UIButton *)button{
    if(self.delegate && [self.delegate respondsToSelector:@selector(didSelectedSubmitButtonInDeviceEditView:)]){
        [self.delegate didSelectedSubmitButtonInDeviceEditView:self];
    }
    [self dismiss];
}
-(void)cancelClick:(UIButton *)button{
    if(self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCancelButtonInDeviceEditView:)]){
        [self.delegate didSelectedCancelButtonInDeviceEditView:self];
    }
    [self dismiss];
}
#pragma mark -


@end
